package com.my.helper.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my.helper.MainActivity;
import com.my.helper.R;
import com.my.helper.activity.ChatActivity;
import com.my.helper.activity.HistoryActivity;
import com.my.helper.activity.LoginActivity;
import com.my.helper.util.CacheActivity;
import com.my.helper.util.SharedPrefUtil;

/**
 * 项目名称：Helper
 * 创建人：
 * 创建时间：2018/5/4 11:29
 */

public class FragmentUser extends Fragment implements View.OnClickListener {
    private View view = null;
    private SharedPrefUtil sp;
    private RelativeLayout rlGoOut;
    private RelativeLayout rlLiShi;
    private RelativeLayout rlHistory;
    private TextView tvUser;
    private RelativeLayout rlChat;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_user, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        sp = new SharedPrefUtil(getActivity(), "myHelper");

        initView();
        initListener();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if (hidden) {// 不在最前端界面显示
        } else {// 重新显示到最前端中
            tvUser.setText(sp.getString("user_name", ""));
        }
    }

    private void initView() {
        rlGoOut = view.findViewById(R.id.rl_tui_chu_user);
        rlLiShi = view.findViewById(R.id.rl_li_shi_user);
        rlHistory = view.findViewById(R.id.rl_li_shi_user);
        tvUser = view.findViewById(R.id.tv_number_user);
        tvUser.setText(sp.getString("user_name", ""));
        rlChat = view.findViewById(R.id.rl_liao_tian_user);
    }


    private void initListener() {
        rlGoOut.setOnClickListener(this);
        rlLiShi.setOnClickListener(this);
        rlHistory.setOnClickListener(this);
        rlChat.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_tui_chu_user: // 退出
                sp.putString("user_id", "");
                sp.commit();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                CacheActivity.finishSingleActivityByClass(MainActivity.class);
                break;
            case R.id.rl_li_shi_user: // 历史
                Intent intent1 = new Intent(getActivity(), HistoryActivity.class);
                startActivity(intent1);
                break;
            case R.id.rl_liao_tian_user:
                ChatActivity.startActivity(getActivity());
                break;
            default:
                break;
        }
    }
}
